package com.ztesoft.ossup.report.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ztesoft.ossup.report.dao.ConditionEnumRepository;
import com.ztesoft.ossup.report.dao.JdbcTemplateDao;
import com.ztesoft.ossup.report.domain.Report;
import com.ztesoft.ossup.report.domain.ReportCondition;
import com.ztesoft.ossup.report.domain.ReportConditionEnum;

@Service("conditionEnumService")
public class ConditionEnumService {

	@Autowired
	JdbcTemplateDao jdbcTemplateDao;
	
	@Autowired
	ConditionEnumRepository conEnumRep;
	
	public List<Map<String, Object>> getItems(){
		Map retMap = null;
		List<Map<String, Object>> retList = null;
		try{
//			ReportConditionEnum reportCondition = conEnumRep.findOne(reportId);
//			String sql = reportCondition.getSql();//对sql进行处理
			String sql = "select id,name,stype,item_val  itemVal ,item_name itemName,sort_order sortOrder from REPORT_CONDITION_ENUM ";
			retList = jdbcTemplateDao.getData(sql);		
			if (!retList.isEmpty()){
				retMap = retList.get(0);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return retList;
	}
	
//	public List<Map<String, Object>> getGroup(){
//		Map retMap = null;
//		List<Map<String, Object>> retList = null;
//		try{
////			ReportConditionEnum reportCondition = conEnumRep.findOne(reportId);
////			String sql = reportCondition.getSql();//对sql进行处理
//			String sql = "select distinct name ,stype from report.REPORT_CONDITION_ENUM";
//			retList = jdbcTemplateDao.getData(sql);		
//			if (!retList.isEmpty()){
//				retMap = retList.get(0);
//			}
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		return retList;
//	}
//	
	@SuppressWarnings("unchecked")
	@Transactional
	public boolean addOrEditConditionEnum(Map<String, Object> conditionEnum) {
		boolean flag = false;
			try {
				ReportConditionEnum conEnum = new ReportConditionEnum();
				if(conditionEnum.containsKey("ID")){
					conEnum.setId(Long.parseLong(conditionEnum.get("ID").toString()));
				}
				conEnum.setName(conditionEnum.get("NAME").toString());
				conEnum.setStype(conditionEnum.get("STYPE").toString());
				conEnum.setItemName(conditionEnum.get("ITEMNAME").toString());
				conEnum.setItemVal(conditionEnum.get("ITEMVAL").toString());
				conEnum.setSortOrder(Integer.parseInt(conditionEnum.get("SORTORDER").toString()));
				ReportConditionEnum backCE = conEnumRep.save(conEnum);
				if(backCE.id>0){
					 flag = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
		return flag;
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public boolean addOrEditItem(List<Map<String, Object>> itemList) {
		boolean flag = true;
		try {
			List<ReportConditionEnum> conEnums = new ArrayList<ReportConditionEnum>();
			for(int i=0;i<itemList.size();i++){
				Map<String, Object> item = itemList.get(i);
				ReportConditionEnum conEnum = new ReportConditionEnum();
				
				Long cId = Long.parseLong(item.get("ID").toString());
				if(cId == 0){
					conEnum.setItemVal(item.get("ITEMVAL").toString());
					conEnum.setItemName(item.get("ITEMNAME").toString());
					conEnum.setSortOrder(Integer.parseInt(item.get("SORTORDER").toString()));
					conEnum.setStype(item.get("STYPE").toString());
					conEnum.setName(item.get("NAME").toString());
					ReportCondition condition = new ReportCondition();
					condition.setId(Long.parseLong(item.get("CONDITIONID").toString()));
//					conEnum.setCondition(condition);
					conEnums.add(conEnum);
				}else{
					ReportConditionEnum c = conEnumRep.findOne(cId);
					c.setItemName(item.get("ITEMNAME").toString());
					c.setItemVal(item.get("ITEMVAL").toString());
					ReportCondition condition = new ReportCondition();
					condition.setId(Long.parseLong(item.get("CONDITIONID").toString()));
//					c.setCondition(condition);
					conEnums.add(c);
				}
			}
			//批量新增操作
			if(conEnums.size()>0){
				List<ReportConditionEnum> backList = conEnumRep.save(conEnums);
				if(backList == null){
					flag = false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
	return flag;
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public boolean deleteItems(String itemIds) {
		boolean flag = true;
		try {
			itemIds = itemIds.substring(1, itemIds.length()-1);
			String[] idsArray = itemIds.split(",");
			for(int i = 0;i<idsArray.length ;i++){
				String id = idsArray[i];
//				conEnumRep.deleteById();
				conEnumRep.delete(Long.parseLong(id));
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	public boolean deleteGroups(String groupIds) {
		boolean flag = true;
		try {
			String str = groupIds;
			str = str.substring(2, str.length()-2);
			String[] idsArray = str.split(",");
			for(int i = 0;i<idsArray.length ;i++){
				String stype = idsArray[i];
				conEnumRep.deleteByStype(stype);;
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	
}
